-- common_status
-- created by zhuangsl
-- 附加通用状态

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 如果免疫了该技能
        if SkillM.immunitySkill(source, target, skillId) then
            if FormulaM.invoke("CAN_TARGET_INJURED", target, skillId) then
                -- 如果不是无敌状态，则播放免疫的效果
                SkillM.getSequence(target):immunity(source, target, skillId);
            end
            return {};
        end

        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[4] > 0 then
            effectRound = round + para[4] + 1;
        end

        local condition = {
            ["value"] = para[3],
            ["end_round"] = effectRound,
        };

        if para[2] > 0 then
            condition["id"] = para[2];
        end

        -- 如果有额外的状态参数
        if #para > 4 then
            local keys   = para[5];
            local values = para[6];
            for i = 1, #keys do
                condition[keys[i]] = values[i];
            end
        end
        
        -- 附加状态
        CombatStatusM.applyStatus(target, para[1], condition);

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        if #arr == 4 then
            return { arr[1], tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), }
        else
            local keys = string.explode(arr[5], "+");
            local values = string.explode(arr[6], "+");

            for index, value in pairs(values) do
                values[index] = tonumber(value);
            end

            return {arr[1], tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), keys, values};
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local value = para[3];
        desc = string.gsub(desc, "{num_value}", value);
        value = math.modf(value / 10);
        desc = string.gsub(desc, "{ratio_value}", value);

        local round = para[4];
        desc = string.gsub(desc, "{round}", round);

        return desc;
    end,
};
